<template>
  <div class="container"
       v-if="history_list.length>0">
    <div class="header clearfix">
      <div class="left">
        搜索历史
      </div>
      <div class="right"
           @tap="clear_history()">
        <img src="https://h5.ameimeika.com/mp_images/mp_2.0/shouye/qingchu@2x.png"
             alt=""
             class="img">
      </div>
    </div>
    <ul class="list clearfix">
      <li class="list-item"
          v-for="(item, index) in history_list"
          :key="index"
          @tap="select_key(item)">
        {{item}}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data () {
    return {
      history_list: []
    };
  },
  methods: {
    get_history () {
      let that = this;
      wx.getStorage({
        key: "search_history",
        success: res => {
          that.history_list = res.data;
        },
        fail: () => { },
        complete: () => { }
      });
    },
    clear_history () {
      this.history_list = [];
      wx.removeStorage({
        key: "search_history",
        success: res => { }
      });
    },
    select_key (item) {
      this.$emit("select_key", item);
    }
  },
  mounted () {
    this.get_history();
  }
};
</script>

<style lang="less" scoped>
.container {
    // margin-bottom: 40px;
  .header {
    margin-top: 15px;
    padding: 0 15px;
    margin-bottom: 10px;
    .left {
      float: left;
      font-size: 14px;
      font-family: "Medium";
      font-weight: 500;
      color: rgba(102, 102, 102, 1);
      line-height: 16px;
    }
    .right {
      float: right;
      .img {
        width: 16px;
        height: 16px;
        display: block;
      }
    }
  }
  .list {
    margin: 0 15px;
    box-sizing: border-box;
    .list-item {
      max-width: 100%;
      line-height: 30px;
      border-radius: 30px;
      padding: 0 10px;
      margin-right: 10px;
      margin-bottom: 10px;
      background: rgba(231, 231, 231, 1);
      font-size: 12px;
      font-family: "PingFangSC-Regular";
      font-weight: 400;
      color: rgba(102, 102, 102, 1);
      float: left;
      box-sizing: border-box;
      overflow: hidden;
      text-overflow: ellipsis;
      -webkit-line-clamp: 1;
      -webkit-box-orient: vertical;
    }
  }
}
</style>

